package com.smartEdu.service.impl.web;

import java.util.ArrayList;
import java.util.List;

import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.ReqMap;
import com.smartEdu.commons.RespMap;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.commons.enums.HomeWorkStatusEnum;
import com.smartEdu.dao.HomeWorkDao;
import com.smartEdu.mapper.common.FileInfoMapper;
import com.smartEdu.mapper.common.HomeWorkDetailMapper;
import com.smartEdu.mapper.common.HomeWorkInfoMapper;
import com.smartEdu.mapper.common.VideoInfoMapper;
import com.smartEdu.po.FileInfoPo;
import com.smartEdu.po.HomeWorkDetailPo;
import com.smartEdu.po.HomeWorkInfoPo;
import com.smartEdu.po.HomeWorkPo;
import com.smartEdu.po.VideoInfoPo;
import com.smartEdu.service.HomeWorkService;
import com.smartEdu.utils.BeanUtil;
import com.smartEdu.utils.JsonUtil;
import com.smartEdu.utils.ToolUtils;
import com.smartEdu.vo.web.HomeWorkVo;

@Service(value="homeWorkServiceImplWeb")
public class HomeWorkServiceImpl implements HomeWorkService{
	
	@Autowired
	@Qualifier(value="homeWorkDaoImpl")
	private HomeWorkDao homeWorkDao;
	
	@Autowired
	private HomeWorkInfoMapper hmInfoMapper;
	
	@Autowired
	private FileInfoMapper fileInfoMapper;
	
	@Autowired
	private VideoInfoMapper videoInfoMapper;
	
	@Autowired
	private HomeWorkDetailMapper hmDetailMapper;

	@Override
	public void GetWarm(Session sess) {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void homeWorkDetail(Session sess) {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void homeworkSubmit(Session sess) {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void homeWork2(Session sess) {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void ShowHomeWork(Session sess) {
		ReqMap map = null;
		List<HomeWorkPo> t = null,s = null;
		try{
			map = new ReqMap();
			map.SetAll(sess.getVals());
			map.put("courseTableName", "course_id_"+Tag_Tips.ACADEMICVALUE);
			t = new ArrayList<HomeWorkPo>();
			s = homeWorkDao.getHomeWorkByTid(map);
			for (HomeWorkPo po : s) {
				po.setStatusName(HomeWorkStatusEnum.getName(po.getStatus()));
				t.add(po.clone());
			}
			sess.put(Tag_Tips.DAODATA, t);
		}
		catch(Exception e) {
			LogCvt.error("获取HomeWork失败",e);
		}
		finally {
//			t.clear();
//			s.clear();
		}
		
	}
	@Transactional(propagation=Propagation.NESTED)
	public void treatmentNewHomeWork(HomeWorkVo vo) throws Exception {
		RespMap map = new RespMap();
		try {
			assemblyFilePo(vo, map);
			assemblyVideoPo(vo, map);
			assemblyHomeWorkInfoPo(vo, map);
			assemblyHomeWorkDetailPo(vo, map);
			for(String s:vo.getXzb()){
				hmInfoMapper.addHomeWorkXzb(map.gets("pk"),Integer.valueOf(s));
			}
		}
		catch(Exception e) {
			LogCvt.error(e.getMessage(), e);
			throw new Exception();
		}
		finally {
			map = null;
		}
	}
	
	// 保存附件信息
	@Transactional(propagation=Propagation.NESTED)
	private void assemblyFilePo(HomeWorkVo vo, RespMap map) throws Exception {
		ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		for (String s : vo.getAttachment()) {
			HashMap<String,String> hmap = new HashMap<>();
			FileInfoPo filePo = new FileInfoPo();
			int idx = s.lastIndexOf(".");
			String format = s.substring(idx+1);
			String name = s.substring(s.substring(0, idx).lastIndexOf("/")+1,idx);
			filePo.setName(name);
			filePo.setFormat(format);
			String pk = ToolUtils.getUUID();
			filePo.setPk(pk);
			filePo.setFtype(1);
			filePo.setCreator(vo.getCreator());
			filePo.setCreatetime((new Date()).getTime()/ 1000);
			filePo.setFurl(s);
			filePo.setFsize(0);
			filePo.setFdesc("test file");
			filePo.setMd5Code("0000");
			filePo.setStatus((short) 1);
			fileInfoMapper.insert(filePo);
			hmap.put("pk",pk);
			hmap.put("name", name);
			list.add(hmap);
		}
		map.put("attachment", list);
	}
	
	//保存视频文件信息
	@Transactional(propagation=Propagation.NESTED)
	private void assemblyVideoPo(HomeWorkVo vo, RespMap map) throws Exception {
		VideoInfoPo videoPo =  new VideoInfoPo();
		String pk = ToolUtils.getUUID();
		videoPo.setPk(pk);
		videoPo.setvType((short)1);
		videoPo.setCreator(vo.getCreator());
		videoPo.setCreateTime((new Date()).getTime() / 1000);
		videoPo.setvUrl(vo.getVideopk());
		videoPo.setFormat(vo.getVideopk().substring(
				vo.getVideopk().lastIndexOf(".")+1));
		videoPo.setvLong(0);
		videoPo.setVsize(0);
		videoPo.setMd5Code("0000");
		videoPo.setStatus((short)1);
		videoInfoMapper.insert(videoPo);
		map.put("videopk", pk);
	}
	
	@Transactional(propagation=Propagation.NESTED)
	private void assemblyHomeWorkInfoPo(HomeWorkVo vo, RespMap map) throws Exception {
		HomeWorkInfoPo po = BeanUtil.copyProperties(vo, HomeWorkInfoPo.class);
		String pk = ToolUtils.getUUID();
		po.setPk(pk);
		po.setAcademic(Tag_Tips.ACADEMICVALUE);
		po.setSrc_id(Integer.valueOf(vo.getXzb()[0]));
		po.setStatus((short)2);
		hmInfoMapper.insert(po);
		map.put("pk", pk);
	}
	
	@Transactional(propagation=Propagation.NESTED)
	private void assemblyHomeWorkDetailPo(HomeWorkVo vo, RespMap map) throws Exception {
		HomeWorkDetailPo po = new HomeWorkDetailPo();
		po.setPk(map.gets("pk"));
		po.setHtype(vo.getHtype());
		po.setContent(vo.getContent());
		po.setExampk(vo.getExampk());
		po.setVideopk(map.gets("videopk"));
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (String s : vo.getTimepoint()) {
			list.add(Integer.valueOf(s.replaceAll(" ", "")));
		}
		po.setTimepoint(JsonUtil.toJson(list));
		po.setAttachment(JsonUtil.toJson(map.geto("attachment")));
		hmDetailMapper.insert(po);
	}
	
}
